package com.antdesignbackend.backend.controller;


import com.antdesignbackend.backend.animalNumber.entity.AnimalNumber;
import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.entity.Enterprise;
import com.antdesignbackend.backend.entity.entityVo.ProductionVo;
import com.antdesignbackend.backend.service.EnterpriseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 天数设置表
 *
 * @author 林宇杰
 * @email setday
 * @date 2020-08-27 10:41:00
 */

@Controller
@RequestMapping("/setday")
@Slf4j
public class SetdayController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private EnterpriseService enterpriseService;
	@Autowired
	private AnimalNumberService animalNumberService;

	@GetMapping("/index")
	@AuthToken
	public String index(Model model) {
		Enterprise enterprise = enterpriseService.getById(getLoginEnterpriseId());
		model.addAttribute("enterprise", enterprise);
		return "setDay/index";
	}

	@PostMapping("/tableSearch")
	@ResponseBody
	public ReturnVO tableSearch() {
		ReturnVO result = new ReturnVO();
		try {
			Map<String,Integer> map=new HashMap();
			Enterprise enterprise=enterpriseService.getById(getLoginEnterpriseId());
			map.put("day1",enterprise.getDay1());
			map.put("day2",enterprise.getDay2());
			result.setData(map);
		}catch (Exception e){
			logger.error(e.getMessage());
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Enterprise enterprise) {
		enterprise.setEnterpriseId(getLoginEnterpriseId());
		ReturnVO result = new ReturnVO();
		if (enterpriseService.updateById(enterprise)) {
			result.setMessage("保存成功！");
			result.setCode("200");
		} else {
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("/selectBatchNumber")
	@ResponseBody
	@AuthToken
	public ReturnVO selectBatchNumber() {
		ReturnVO returnVO = new ReturnVO();
			QueryWrapper<AnimalNumber>  animalNumberQueryWrapper=new QueryWrapper<>();
			animalNumberQueryWrapper.eq("eid",getLoginEnterpriseId()).orderByDesc("starTime");
			List<AnimalNumber> animalNumbers=animalNumberService.list(animalNumberQueryWrapper);
			List<Map<String,Object>> maps=new ArrayList<>();
			for (AnimalNumber animalNumber:animalNumbers){
				Map<String,Object> stringStringMap=new HashMap<>();
				stringStringMap.put("id",animalNumber.getId());
				stringStringMap.put("batchNumber",animalNumber.getBatchNumber());
				maps.add(stringStringMap);
			}
			returnVO.setData(maps);
			System.out.println("企业所有动物明细"+animalNumbers);
			return  returnVO;
	}
}